
let app = require('express')();
let path = require('path')
var url  = require('url');
var fs   = require('fs');
let cookieParser = require('cookie-parser');
let session = require('express-session');
let bodyParser = require("body-parser"); 
let log = require('../util/log.js');
let login = require('./login.js');
let pay = require('./pay.js');
let api = require('./api.js');
let wechat = require('./wechat.js');
let gameData = require('../util/gameData.js');
let util = require('../util/util.js');
let rpc = require('../util/rpc.js');
var xmlBodyParser = require('express-xml-parser');

let argv = process.argv.splice(2);
gameData.init();
util.setServerInfo('http', argv[0]);

app.use(bodyParser.urlencoded({extended: false}));

app.use(cookieParser());
app.use(session({
    secret: '12345',
    //name: 'testapp',   //这里的name值得是cookie的name，默认cookie的name是：connect.sid
    cookie: {maxAge: (1000 * 60 * 15)},  //设置maxAge是80000ms，即80s后session和相应的cookie失效过期
    resave: false,
    saveUninitialized: true,
}));
// 解决微信支付通知回调数据
app.use('/payCallback', xmlBodyParser({
  type: 'text/xml',
  limit: '1mb'
}));

app.listen(util.getServerInfo('http'), function () {
    log.p('登陆和充值服务器HTTP启动:' + util.getServerInfo('http'));
    rpc.dc.connect('loginId:' + util.getServerInfo('id'));
});
//建立登陆路由
app.get('/login', login.run);
app.post('/login', login.run);
//建立购买回调路由 
app.post('/payCallback', pay.callback);
//建立二维码路由
app.get('/qrCode', api.qrCode);
//后台拉取游戏
app.get('/getGames', api.getGames);
//后台拉取游戏
//app.get('/killGame', api.killGame); 
app.post('/killGame', api.killGame);
//弹窗消息
//app.get('/alertMsg',api.alertMsg);
app.post('/alertMsg',api.alertMsg);
//微信验证
app.get('/wechat/login', wechat.login);
app.post('/wechat/login', wechat.login);
app.get('/wechat/callback', wechat.callback);
//微信分享获取
app.get('/wechat/share', wechat.share);
app.post('/wechat/share', wechat.share);
app.get('/wechat', wechat.run);

//建立基础路由
var mine = {
    "css": "text/css",
    "gif": "image/gif",
    "html": "text/html",
    "ico": "image/x-icon",
    "jpeg": "image/jpeg",
    "jpg": "image/jpeg",
    "js": "text/javascript",
    "json": "application/json",
    "pdf": "application/pdf",
    "png": "image/png",
    "svg": "image/svg+xml",
    "swf": "application/x-shockwave-flash",
    "tiff": "image/tiff",
    "txt": "text/plain",
    "wav": "audio/x-wav",
    "mp3": "audio/x-mp3",
    "wma": "audio/x-ms-wma",
    "wmv": "video/x-ms-wmv",
    "xml": "text/xml",
    "zip": "application/octet-stream"
};

app.get('*', function (req, res) {
    var pathname = url.parse(req.url).pathname; 
    if(pathname=="/ddz/" || pathname=="/ddz"){
        pathname = "ddz/index.html";
    }   
    var realPath = path.join("./web/", pathname);
    var ext = path.extname(realPath);
    ext = ext ? ext.slice(1) : 'unknown';
    if (mine[ext]){
        fs.exists(realPath, function (exists) {
            if (!exists) {
                res.send('No Found').end();
            } else {
                fs.readFile(realPath, "binary", function (err, file) {
                    if (err) {
                        res.send('No Found').end();
                    } else {
                        var contentType = mine[ext] || "application/octet-stream";
                        res.writeHead(200, {
                                        'Content-Type': contentType
                                        });
                        res.write(file, "binary");
                        res.end();
                    }
                });
            }
        });
    }else{
        res.send('No Found').end();
    }
});
app.post('*', function (req, res) {
    res.send('No Found').end();
});